{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 创建列表\n",
    "faceListId = \"hnh05\" # 自己命名\n",
    "create_facelists_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/hnh05\"# 注意此条url修改\n",
    "subscription_key = \"2eda3f4d037f42929d6e5da5a93f7ffc\"\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # 请求头部\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key':subscription_key ,\n",
    "}\n",
    "data = {\n",
    "    \"name\": \"facelist\",\n",
    "    \"userData\": \"明星脸相册\",\n",
    "    \"recognitionModel\": \"recognition_03\",\n",
    "    \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url,headers=headers,json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查你的facelist的信息\n",
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/hnh05\"# 学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [],\n",
       " 'faceListId': 'hnh05',\n",
       " 'name': 'facelist',\n",
       " 'userData': '明星脸相册'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/hnh04/persistedfaces\"\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # 请求头部\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://huangnanhui.gitee.io/api-job/image/hnh.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"hnh\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [],\n",
       " 'faceListId': 'hnh05',\n",
       " 'name': 'facelist',\n",
       " 'userData': '明星脸相册'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查你的facelist的信息\n",
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/hnh05\"\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片/函数——可以重复使用相同的功能\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/oh.jpeg\",\"欧豪\")\n",
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/xk.jpeg\",\"许凯\")\n",
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/lhr.jpg\",\"刘昊然\")\n",
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/wyf.jpg\",\"吴亦凡\")\n",
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/wl.jpg\",\"吴磊\")\n",
    "AddFace(\"http://huangnanhui.gitee.io/api-job/image/mty.jpg\",\"马天宇\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '624d01cd-0e60-4c54-a9c3-55e04215deab',\n",
       "   'userData': 'hnh'},\n",
       "  {'persistedFaceId': '43e52313-a32c-4737-8005-5f48c4d417a6',\n",
       "   'userData': '欧豪'},\n",
       "  {'persistedFaceId': 'e5a6a679-8fd4-4a85-b6c7-3b24b8e87850',\n",
       "   'userData': '许凯'},\n",
       "  {'persistedFaceId': '2024508c-6a34-4f5a-ac7f-341ea7e9d108',\n",
       "   'userData': '刘昊然'},\n",
       "  {'persistedFaceId': '5efdd99a-1f90-4b11-975f-e73457557128',\n",
       "   'userData': '吴亦凡'},\n",
       "  {'persistedFaceId': 'dbd6d75b-8b65-4d51-95e4-2782b1c1e684',\n",
       "   'userData': '吴磊'},\n",
       "  {'persistedFaceId': 'd34d959a-9f8e-4c78-9a4b-a19b04784f38',\n",
       "   'userData': '马天宇'},\n",
       "  {'persistedFaceId': '587fd9f1-0046-4a77-b0f1-55657d26385a',\n",
       "   'userData': 'hnh'}],\n",
       " 'faceListId': 'hnh04',\n",
       " 'name': 'facelist',\n",
       " 'userData': '明星脸相册'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查你的facelist的信息\n",
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/hnh04\"\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'e4c606e7-940f-4eae-a9e0-b087fcda5a98',\n",
       "  'faceRectangle': {'top': 368, 'left': 514, 'width': 218, 'height': 218},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 3.7, 'roll': 16.8, 'yaw': 23.2},\n",
       "   'gender': 'male',\n",
       "   'age': 18.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.001},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.63},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.28},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': True},\n",
       "   'accessories': [{'type': 'glasses', 'confidence': 0.98}],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.13,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 0.95},\n",
       "     {'color': 'brown', 'confidence': 0.91},\n",
       "     {'color': 'other', 'confidence': 0.33},\n",
       "     {'color': 'gray', 'confidence': 0.2},\n",
       "     {'color': 'blond', 'confidence': 0.18},\n",
       "     {'color': 'red', 'confidence': 0.15},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Detect 检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://1119api.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://huangnanhui.gitee.io/api-job/image/hnh.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":'e4c606e7-940f-4eae-a9e0-b087fcda5a98',#取上方的faceID\n",
    "    \"faceListId\": \"hnh05\",\n",
    "#     \"faceIds\":faceId_02,\n",
    "    \"maxNumOfCandidatesReturned\": 6,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': 'c1790a86-3994-4760-9151-e727ec479362',\n",
       "  'confidence': 0.26577},\n",
       " {'persistedFaceId': 'b9aa1442-417a-491b-b858-8651874bc422',\n",
       "  'confidence': 0.13272},\n",
       " {'persistedFaceId': '41544b76-5444-4640-b0a3-6c12f592cc96',\n",
       "  'confidence': 0.09703},\n",
       " {'persistedFaceId': '82c08dc9-29be-444f-ba7a-c9faf42dff0e',\n",
       "  'confidence': 0.09435},\n",
       " {'persistedFaceId': '7faead49-f30c-4b34-ba13-0449741a5b93',\n",
       "  'confidence': 0.09128},\n",
       " {'persistedFaceId': '66095e01-57e7-4464-afc9-14f70b0ae8ce',\n",
       "  'confidence': 0.09124}]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>624d01cd-0e60-4c54-a9c3-55e04215deab</td>\n",
       "      <td>hnh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>43e52313-a32c-4737-8005-5f48c4d417a6</td>\n",
       "      <td>欧豪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>e5a6a679-8fd4-4a85-b6c7-3b24b8e87850</td>\n",
       "      <td>许凯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2024508c-6a34-4f5a-ac7f-341ea7e9d108</td>\n",
       "      <td>刘昊然</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5efdd99a-1f90-4b11-975f-e73457557128</td>\n",
       "      <td>吴亦凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>dbd6d75b-8b65-4d51-95e4-2782b1c1e684</td>\n",
       "      <td>吴磊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d34d959a-9f8e-4c78-9a4b-a19b04784f38</td>\n",
       "      <td>马天宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>587fd9f1-0046-4a77-b0f1-55657d26385a</td>\n",
       "      <td>hnh</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData\n",
       "0  624d01cd-0e60-4c54-a9c3-55e04215deab      hnh\n",
       "1  43e52313-a32c-4737-8005-5f48c4d417a6       欧豪\n",
       "2  e5a6a679-8fd4-4a85-b6c7-3b24b8e87850       许凯\n",
       "3  2024508c-6a34-4f5a-ac7f-341ea7e9d108      刘昊然\n",
       "4  5efdd99a-1f90-4b11-975f-e73457557128      吴亦凡\n",
       "5  dbd6d75b-8b65-4d51-95e4-2782b1c1e684       吴磊\n",
       "6  d34d959a-9f8e-4c78-9a4b-a19b04784f38      马天宇\n",
       "7  587fd9f1-0046-4a77-b0f1-55657d26385a      hnh"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "#facelist里面的数据\n",
    "faceListId_df = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "faceListId_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c1790a86-3994-4760-9151-e727ec479362</td>\n",
       "      <td>0.26577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b9aa1442-417a-491b-b858-8651874bc422</td>\n",
       "      <td>0.13272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41544b76-5444-4640-b0a3-6c12f592cc96</td>\n",
       "      <td>0.09703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>82c08dc9-29be-444f-ba7a-c9faf42dff0e</td>\n",
       "      <td>0.09435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7faead49-f30c-4b34-ba13-0449741a5b93</td>\n",
       "      <td>0.09128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>66095e01-57e7-4464-afc9-14f70b0ae8ce</td>\n",
       "      <td>0.09124</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  c1790a86-3994-4760-9151-e727ec479362     0.26577\n",
       "1  b9aa1442-417a-491b-b858-8651874bc422     0.13272\n",
       "2  41544b76-5444-4640-b0a3-6c12f592cc96     0.09703\n",
       "3  82c08dc9-29be-444f-ba7a-c9faf42dff0e     0.09435\n",
       "4  7faead49-f30c-4b34-ba13-0449741a5b93     0.09128\n",
       "5  66095e01-57e7-4464-afc9-14f70b0ae8ce     0.09124"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "find_df = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "find_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [persistedFaceId, userData, confidence]\n",
       "Index: []"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(faceListId_df, find_df,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
